package com.longjianghuasheng.logistics.business.order.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.order.viewmodel.OrderViewModel
import com.longjianghuasheng.logistics.databinding.FragmentOrderInBinding
import com.longjianghuasheng.logistics.eventViewModel

class OrderInFragment : BaseFragment<OrderViewModel, FragmentOrderInBinding>() {


    private var type = 0

    private var outIndex = 0

    private val orderInPageAdapter by lazy {

        OrderInPageAdapter(this)
    }

    private val titles by lazy {
        resources.getStringArray(R.array.order_in_tab).toMutableList()
    }


    private val fragments by lazy {
        arrayListOf<Fragment>()
    }

    override fun layoutId(): Int = R.layout.fragment_order_in

    override fun initView(savedInstanceState: Bundle?) {

        initTabAndViewPager()

    }

    private fun initTabAndViewPager() {


        mDatabind.run {


            for ((index, s) in titles.withIndex()) {
                if (outIndex != 0 && index == 1) continue

                tabLayout.addTab(tabLayout.newTab().setText(s))

                fragments.add(OrderFragment.newInstance(type, index, outIndex))
            }




            viewPager.run {

                isUserInputEnabled = false

                offscreenPageLimit = titles.size

                adapter = orderInPageAdapter

                tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                    override fun onTabSelected(tab: TabLayout.Tab?) {


                        setCurrentItem(tab?.position ?: 0, false)

                        eventViewModel.onOrderChildSelectEvent.value = tab?.position ?: 0

                    }

                    override fun onTabUnselected(tab: TabLayout.Tab?) {

                    }

                    override fun onTabReselected(tab: TabLayout.Tab?) {

                    }
                })


            }


            tabLayout.getTabAt(0)
                ?.select()

        }

    }

    override fun createObserver() {
        super.createObserver()

    }

    override fun initData() {
        super.initData()
        mViewModel.type = type
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    companion object {

        const val TYPE_IMPORT = "import"
        const val TYPE_EXPORT = "export"
        const val TYPE_INSPECTION = "inspection"

        fun newInstance(type: Int, index: Int): OrderInFragment {
            return OrderInFragment().also {
                it.type = type
                it.outIndex = index
            }
        }
    }

    inner class OrderInPageAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]
    }
}